﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iCollect.Constants;
using System.Collections;
using System.IO;

namespace iCollect.util
{
    class FileUtil
    {
        private static FileUtil fileUtil;
        public static FileUtil getInstance()
        {
            if (fileUtil == null)
            {
                fileUtil = new FileUtil();
            }
            return fileUtil;
        }
        private void writeChannelData(string key, List<ArrayList> data, string fileName)
        {
            if (File.Exists(fileName))
            {
                using (StreamWriter sw = File.AppendText(fileName))
                {
                    writeData2File(data, sw);
                    sw.Close();
                }
            }
            else
            {
                using (StreamWriter sw = File.CreateText(fileName))
                {
                    writeData2File(data, sw);
                    sw.Close();
                }
            }

        }

        private void writeData2File(List<ArrayList> data, StreamWriter sw)
        {
            string dataValue = DateTime.Now.ToLongTimeString() + "," + data[data.Count - 1][1].ToString();
            sw.WriteLine(dataValue);
        }

        public void writeData()
        {
            string thisYear = DateTime.Today.Year.ToString();
            string dateStr = DateTime.Today.ToShortDateString().Replace("/","_");
            string dirName = "Device\\" + GlobalConstants.currentDevice + 
                "\\正常" + "\\" + thisYear + "\\" + dateStr; 
            string fileName;
            // Delete the file if it exists.
            if (!System.IO.Directory.Exists(dirName))
            {
                System.IO.Directory.CreateDirectory(dirName);
            }

            for (int i = 0; i < GlobalConstants.currentData.Keys.Count; i++)
            {
                
                String key = GlobalConstants.currentData.Keys.ElementAt(i);
                fileName = dirName + "\\" + key;
                writeChannelData(key, GlobalConstants.currentData[key],fileName);
            }

        }

        public void writeDataManual()
        {
            string thisYear = DateTime.Today.Year.ToString();
            string dateStr = DateTime.Today.ToShortDateString().Replace("/", "_");
            string dirName = "Device\\" + GlobalConstants.currentDevice +
                "\\临时存储" + "\\" + thisYear + "\\" + dateStr;
            string fileName;
            // Delete the file if it exists.
            if (!System.IO.Directory.Exists(dirName))
            {
                System.IO.Directory.CreateDirectory(dirName);
            }

            for (int i = 0; i < GlobalConstants.currentData.Keys.Count; i++)
            {

                String key = GlobalConstants.currentData.Keys.ElementAt(i);
                fileName = dirName + "\\" + key;
                writeAllChannelData(key, GlobalConstants.currentData[key], fileName);
            }

        }

        private void writeAllChannelData(string key, List<ArrayList> data, string fileName)
        {
            if (File.Exists(fileName))
            {
                using (StreamWriter sw = File.AppendText(fileName))
                {
                    writeAllData2File(data, sw);
                    sw.Close();
                }
            }
            else
            {
                using (StreamWriter sw = File.CreateText(fileName))
                {
                    writeAllData2File(data, sw);
                    sw.Close();
                }
            }

        }

        private void writeAllData2File(List<ArrayList> data, StreamWriter sw)
        {
            sw.Write(DateTime.Now.ToLongTimeString());
            foreach (ArrayList d in data)
            {
                string dataValue =  "," + d[1].ToString();
                sw.Write(dataValue);
            }
            sw.WriteLine();
        }
    }
}
